/***********************************
* Name: Input.java
* Date: Aug 7, 2010
* @author martin
* @author karen
* Description:
***********************************/

import javax.swing.JDialog; 
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.*;

public class CustomDialog extends JDialog implements ActionListener {
    
    private JPanel myPanel = null;
    private JPanel infoPanel = null;
    private JPanel posPanel = null;
    public  JSpinner coordX = null;
    public  JSpinner coordY = null;
    private JPanel inputA = null;
    private JPanel fromPanelA = null;
    public  JComboBox fromA = null;
    private JPanel tempPanelA = null;
    public  JSpinner fdA = null;
    public  JSpinner rdA = null;
    private JPanel inputB = null;
    private JPanel fromPanelB = null;
    public  JComboBox fromB = null;
    private JPanel tempPanelB = null;
    public  JSpinner fdB = null;
    public  JSpinner rdB = null;
    private JPanel lastPanel = null;
    private JButton deleteButton = null;
    //public  JCheckBox isfinal = null;
    private JPanel okPanel = null;
    private JButton okButton = null;
    private JButton cancelButton = null;

    public  boolean gateDeleted = false;
    public  boolean okbutton = false;
    public  boolean cancelbutton = true;

    //String name, String type, x, y, fallDelay_A, raiDelay_A, ComboBox from_A, fallDelay_B, raiDelay_B,ComboBox from_B, end
    public CustomDialog(Frame frame, String gateName, String gateType, int xCoord, int yCoord, JComboBox sentFromA, int fallDelayA, int raiDelayA, JComboBox sentFromB, int fallDelayB, int raiDelayB) {
		
                //this.set//
        super(frame, true);
        setLocationRelativeTo(frame);
                myPanel = new JPanel();
		myPanel.setLayout(new BoxLayout(myPanel, BoxLayout.Y_AXIS));
		
		getContentPane().add(myPanel);
		infoPanel = new JPanel();
		infoPanel.add(new JLabel("Name: " + gateName));
		infoPanel.add(Box.createRigidArea(new Dimension(20,10)));
		infoPanel.add(new JLabel("Type: " + gateType));
		myPanel.add(Box.createRigidArea(new Dimension(0,5)));
		myPanel.add(infoPanel);
		
		posPanel = new JPanel();
		posPanel.add(new JLabel("Position "));
		posPanel.add(new JLabel("X: "));
		//X Coords
		coordX = new JSpinner();
		coordX.setValue(xCoord);
		coordX.addChangeListener(new ChangeListener(){
			@Override
			public void stateChanged(ChangeEvent e) {
				System.err.println("X coordinate: "+coordX.getValue().toString());
			}
		});	
		posPanel.add(coordX);
		
		//Y Coords
		posPanel.add(new JLabel("Y: "));
		coordY = new JSpinner();
		coordY.setValue(yCoord);
		coordY.addChangeListener(new ChangeListener(){
			@Override
			public void stateChanged(ChangeEvent e) {
				System.err.println("Y coordinate: "+coordY.getValue().toString());
			}
		});
		posPanel.add(coordY);
		myPanel.add(Box.createRigidArea(new Dimension(0,5)));
		myPanel.add(posPanel);
		
		inputA = new JPanel();
		inputA.setBorder(BorderFactory.createTitledBorder("Input A"));
		inputA.setLayout(new BoxLayout(inputA, BoxLayout.Y_AXIS));
		fromPanelA = new JPanel();
		fromPanelA.add(new JLabel("From: "));
		fromA = new JComboBox();
		fromA = sentFromA;
		fromPanelA.add(fromA);
		inputA.add(fromPanelA);
		tempPanelA = new JPanel();
		tempPanelA.setLayout(new BoxLayout(tempPanelA, BoxLayout.X_AXIS));
		tempPanelA.add(new JLabel("Falling delay: "));
		fdA = new JSpinner();
		fdA.setValue(fallDelayA);
		fdA.addChangeListener(new ChangeListener(){
			@Override
			public void stateChanged(ChangeEvent e) {
				System.err.println("Falling DelayA: "+fdA.getValue().toString());
			}
		});
		tempPanelA.add(fdA);
		tempPanelA.add(Box.createRigidArea(new Dimension(10,5)));
		tempPanelA.add(new JLabel("Rising delay: "));
		rdA = new JSpinner();
		rdA.setValue(raiDelayA);
		rdA.addChangeListener(new ChangeListener(){
			@Override
			public void stateChanged(ChangeEvent e) {
				System.err.println("Rising DelayA: "+rdA.getValue().toString());
			}
		});
		tempPanelA.add(rdA);
		inputA.add(tempPanelA);
		myPanel.add(Box.createRigidArea(new Dimension(0,5)));
		myPanel.add(inputA);
		
		inputB = new JPanel();
                inputB.setBorder(BorderFactory.createTitledBorder("Input B"));
		inputB.setLayout(new BoxLayout(inputB, BoxLayout.Y_AXIS));
		fromPanelB = new JPanel();
		fromPanelB.add(new JLabel("From: "));
		fromB = new JComboBox();
		fromB = sentFromB;
		fromPanelB.add(fromB);
		inputB.add(fromPanelB);
		tempPanelB = new JPanel();
		tempPanelB.setLayout(new BoxLayout(tempPanelB, BoxLayout.X_AXIS));
		tempPanelB.add(new JLabel("Falling delay: "));
		fdB = new JSpinner();
		fdB.setValue(fallDelayB);
		fdB.addChangeListener(new ChangeListener(){
			@Override
			public void stateChanged(ChangeEvent e) {
				System.err.println("Falling DelayB: "+fdB.getValue().toString());
			}
		});
		tempPanelB.add(fdB);
		tempPanelB.add(Box.createRigidArea(new Dimension(10,5)));
		tempPanelB.add(new JLabel("Rising delay: "));
		rdB = new JSpinner();
		rdB.setValue(raiDelayA);
		rdB.addChangeListener(new ChangeListener(){
			@Override
			public void stateChanged(ChangeEvent e) {
				System.err.println("Falling DelayB: "+rdB.getValue().toString());
			}
		});
		tempPanelB.add(rdB);
		inputB.add(tempPanelB);
		myPanel.add(Box.createRigidArea(new Dimension(0,5)));
		

                if(gateType.compareToIgnoreCase("not")!=0){
                    myPanel.add(inputB);

                }

		StyleListener list = new StyleListener();
		lastPanel = new JPanel();
		//isfinal = new JCheckBox("is Final",end);
		//isfinal.addItemListener(list);
		//lastPanel.add(isfinal);
		lastPanel.add(Box.createRigidArea(new Dimension(20,20)));
		deleteButton = new JButton("Delete Gate",Main.iconTrash);
		deleteButton.addActionListener(this);
		lastPanel.add(deleteButton);	
		myPanel.add(Box.createRigidArea(new Dimension(0,5)));
		myPanel.add(lastPanel);
		
		okPanel = new JPanel();
		okButton = new JButton("Ok", Main.iconOk);
		okButton.addActionListener(this);
		okPanel.add(okButton);	
		okPanel.add(Box.createRigidArea(new Dimension(40,20)));
		cancelButton = new JButton("Cancel", Main.iconCancel);
		cancelButton.addActionListener(this);
		okPanel.add(cancelButton);
		myPanel.add(Box.createRigidArea(new Dimension(0,5)));
		myPanel.add(okPanel);
		myPanel.add(Box.createRigidArea(new Dimension(0,5)));
		
		pack();
		setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
		if(deleteButton == e.getSource()) {
			System.err.println("Gate Deleted");
                        setVisible(false);
                        gateDeleted = true;
                        okbutton = false;
                        cancelbutton = false;
		}
		else if(okButton == e.getSource()) {
			System.err.println("Ok");
			setVisible(false);
                        gateDeleted = false;
                        okbutton = true;
                        cancelbutton = false;
		}
		else if(cancelButton == e.getSource()) {
			System.err.println("Cancel");
			setVisible(false);
                        gateDeleted = false;
                        okbutton = false;
                        cancelbutton = true;
		}
    }
	
	private class StyleListener implements ItemListener{
		public void itemStateChanged(ItemEvent event){
			//change state of the gate to final gate
		}
	}
    
}

